import { defineStore } from 'pinia'
import { getSystemInfo } from "@tarojs/taro";
import { oemArea, oemInfo, getInfo } from '@/api/index';
import { useUserStore } from '@/stores'

export const useConfigStore = defineStore('config', {
  state: () => ({
    tabIndex: 0,
    statusBar: 0, // 状态栏的高度
    capsule: 40, // 微信胶囊的高度
    oemArea: [],
    oemInfo: {},
    cityCode: [], // 默认选中的城市 [410000, 410300, 410323]
    cityName: '',
    inviterActivity: null,
  }),
  actions: {
    setSelecTab(i) {
      this.tabIndex = i
    },
    setCity(codeArr, name){
      this.cityCode = codeArr
      this.cityName = name
    },
    setInviterActivity(data){
        this.inviterActivity = data
    },
    async getHeaderHeight(){
      getSystemInfo().then((result) => {
        const { statusBarHeight } = result;
        const { height, top } = wx.getMenuButtonBoundingClientRect();
        this.statusBar = statusBarHeight;
        this.capsule = (top - statusBarHeight) * 2 + height;
      })
    },
    async getOemArea(area_code){  // 获取当前oem下辖区域
      const areaCode = area_code;
      try {
          const res = await oemArea()
          if(res.data){
            const data = res.data;
            this.oemArea = data;
            // 获取到地区code
            if(areaCode){
              for(let province of data){
                for(let city of province.children){
                  // if(!city.children) break;
                  if(city.children){
                    const findArea = city.children.find(area=>area.id == Number(areaCode))
                    if(findArea){
                      // 匹配到OEM的所在区域
                      this.cityCode = [city.pid, city.id, findArea.id]
                      this.cityName = findArea.name
                      break;
                    }
                  } else { // 类似广东东莞只有两级的处理 
                    if(Number(areaCode)===city.id){
                      this.cityCode = [city.pid, city.id]
                      this.cityName = city.name
                      break;
                    }
                  }
                }
              }
            }
            // 未匹配到对应的区域 或 没有拿到地区code
            if(!this.cityName||!areaCode){
              const first = data[0];
              const last = first.children[0].children[0]
              this.cityCode = [ first.id, first.children[0].id, last.id ]
              this.cityName = last.name
            }
          }
          return res;
      } catch (error) {
          throw error
      }
    },
    async getOemInfo(){ // 获取OEM信息
      try {
          const user = useUserStore()
          const res = await oemInfo(user.location)
          if(res.data){
            const { area_code, admin_config, get_logo, project_name, qiniu_url } = res.data;
            const { share_title, share_img, photo_is_auth, star_is_auth, cs_img, cs_phone, upload, star_contract } = admin_config
            this.oemInfo = {
              project_name,
              logo: get_logo.full_path,
              share_title: share_title||'',
              share_img: share_img.full_path||'',
              photo_is_auth,
              star_is_auth,
              qiniu_url,
              cs_img: cs_img.full_path||'',
              cs_phone,
              banner: upload,
              star_contract
            }
            return area_code;
          }else{
            return false;
          }
      } catch (error) {
          throw error
      }
    },
    async getInviteInfo(){
        try {
            const res = await getInfo()
            if (res&&res.data) {
                this.setInviterActivity(res.data.inviterActivity)
                return res.data
            }
        } catch (error) {
            throw error
        }
    }
  }
})

export default useConfigStore